package wk.demo.tool;

import android.graphics.Bitmap;
import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileTool {

    public static void saveMyBitmapToSd(Bitmap mBitmap , File f) {
        // 如果需要保存 bitmap 到本地查看效果。可以使用以下代码
        try {
            f.createNewFile();
        } catch (IOException e) {

        }
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 判断是否存在sd卡
     */
    private static boolean isExistSDCard()  {
        return  Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ;
    }

    /**
     * 获得 SD卡路径
     */
    public static File getExternalSdPath()  {
        return Environment.getExternalStorageDirectory();
    }


    /**
     * 获取内置Sd卡路径
     * @return File
     */
    public static File getInternalSdPath()  {
        return  Environment.getDataDirectory();
    }


    public  static   String getVideoDir() {
        String   lastPath = isExistSDCard()?  getExternalSdPath().getAbsolutePath()  :  getInternalSdPath().getAbsolutePath();
        lastPath+="/zijkPics/" ;
        File file = new File(lastPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return lastPath;
    }
}
